home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / URL Helper II / Source / MirrorLists.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-14  |  6.1 KB  |  330 lines  |  [TEXT/MMCC]

  1. /***
  2.  * MirrorLists.c
  3.  *
  4.  *  Manage the mirror lists at a high level
  5.  *
  6.  ***/
  7.  
  8. #include "debug_me.h"
  9. #include "Exceptions.h"
  10. #include "debug_me.h"
  11. #include "URLHelperComponent.h"
  12. #include "URLHelperComponentPrivate.h"
  13. #include "MemoryBuffer.h"
  14. #include "TextUtils.h"
  15.  
  16. /**
  17.  ** string buffer size is the size of the strings that we will be storing in our own little
  18.  ** private string area.
  19.  **/
  20.  
  21. #define STRING_BUFFER_SIZE    512
  22.  
  23. /**
  24.  * _URLHelperNewMirrorList
  25.  *
  26.  *  Add a new mirror list to our list of mirrors!
  27.  *
  28.  **/
  29. pascal ComponentResult _URLHelperNewMirrorList (Handle storage, StringPtr string,
  30.                                 URLHMirrorListPtr *ref)
  31. {
  32.     OSErr                theErr;
  33.     URLHMirrorListPtr    newList;
  34.     URLGlobalsHandle    globals = (URLGlobalsHandle) storage;
  35.  
  36.     /**
  37.      ** Check the inputs here...
  38.      **/
  39.  
  40.     theErr = paramErr;
  41.     require (ref != 0 && string != 0, BadParams);
  42.  
  43.     /**
  44.      ** Ok.  Create a new mirror list and allocate a string buffer...
  45.      **/
  46.  
  47.     newList = (URLHMirrorListPtr) NewPtrClear (sizeof (URLHMirrorList));
  48.     theErr = MemError ();
  49.     if (theErr == noErr && newList == 0)
  50.         theErr = memFullErr;
  51.     nrequire (theErr, FailedToAllocateNewList);
  52.  
  53.     theErr = NewMemoryBuffer (&(newList->stringBuffer), STRING_BUFFER_SIZE, STRING_BUFFER_SIZE);
  54.     nrequire (theErr, FailedToAllocateStringBuffer);
  55.  
  56.     /**
  57.      ** Save the mirror list name...
  58.      **/
  59.  
  60.     theErr = MBSaveString (newList->stringBuffer, (char *) &(string[1]), string[0],
  61.                             &(newList->oName));
  62.     nrequire (theErr, FailedToSaveListName);
  63.  
  64.     /**
  65.      ** set the rest of the flags...
  66.      **/
  67.     
  68.     newList -> listStatus = kMLModified;
  69.  
  70.     /**
  71.      ** Finally, add the damm thing to the master list
  72.      **/
  73.  
  74.     newList -> next = (*globals)->mirrorList;
  75.     (*globals)->mirrorList = newList;
  76.  
  77.     /**
  78.      ** Done!
  79.      **/
  80.     
  81.     *ref = newList;
  82.     return noErr;
  83.     
  84.     /**
  85.      ** Errors
  86.      **/
  87.  
  88. FailedToSaveListName:                // MB couldn't save the name!
  89.  
  90.     DisposeMemoryBuffer (newList->stringBuffer);
  91.     newList -> stringBuffer = 0;
  92.  
  93. FailedToAllocateStringBuffer:        // Error getting the string buffer up and going!
  94.  
  95.     DisposPtr ((Ptr) newList);
  96.  
  97. FailedToAllocateNewList:            // Couldn't allocate new struct!!!!
  98.  
  99. BadParams:
  100.  
  101.     *ref = 0;
  102.     return theErr;
  103. }
  104.  
  105. /**
  106.  * _URLHGetMirrorList
  107.  *
  108.  *  Find (if we can) a named mirror list.  When we compare mirror names we ignore
  109.  *  case and diatrical marks.
  110.  *
  111.  **/
  112. pascal ComponentResult _URLHelperGetMirrorList (Handle storage,
  113.                                         StringPtr            string,
  114.                                         URLHMirrorListPtr    *ref)
  115. {
  116.     OSErr                theErr;
  117.     URLGlobalsHandle    globals = (URLGlobalsHandle) storage;
  118.     URLHMirrorListPtr    p;
  119.     Str255                mName;
  120.  
  121.     /**
  122.      ** Check the params...
  123.      **/
  124.     
  125.     theErr = paramErr;
  126.     require (string != 0 && ref != 0, BadParams);
  127.  
  128.     /**
  129.      ** Loop through the list of mirrors, and find a match
  130.      **/
  131.  
  132.     p = (*globals)->mirrorList;
  133.     while (p) {
  134.     
  135.         theErr = MBGetPString (p->stringBuffer, p->oName, mName);
  136.         nrequire (theErr, FailedToGetMirrorName);
  137.  
  138.         if (EqualString (string, mName, false, false))
  139.             break;
  140.  
  141.         p = p->next;
  142.     }
  143.  
  144.     /**
  145.      ** Ok -- done...
  146.      **/
  147.     
  148.     *ref = p;
  149.     if (p == 0)
  150.         theErr = errURLHNoSuchMirrorList;
  151.  
  152.     return theErr;
  153.  
  154.     /**
  155.      ** Errors
  156.      **/
  157. FailedToGetMirrorName:
  158.  
  159. BadParams:
  160.     
  161.     *ref = 0;
  162.     return theErr;
  163. }
  164.  
  165. /**
  166.  * _URLHelperAddMirror
  167.  *
  168.  *  Add a mirror to a given mirror list.
  169.  *
  170.  **/
  171. pascal ComponentResult _URLHelperAddMirror (Handle storage,
  172.                                         URLHMirrorListPtr    ref,
  173.                                         StringPtr            mirrorName,
  174.                                         URLHMirrorFlags        flags)
  175. {
  176.     OSErr                theErr;
  177.     URLGlobalsHandle    globals = (URLGlobalsHandle) storage;
  178.     URLHMirrorPtr        theMirror;
  179.  
  180.     /**
  181.      ** Make sure the params are ok!
  182.      **/
  183.  
  184.     theErr = paramErr;
  185.     require (ref != 0 && mirrorName != 0, BadParams);
  186.  
  187.     /**
  188.      ** Allocate the struct for the new mirror.  Put the mirror string name
  189.      ** (with wildcards!) into the string storage for this mirror!
  190.      **/
  191.  
  192.     theMirror = (URLHMirrorPtr) NewPtrClear (sizeof(URLHMirror));
  193.     theErr = MemError ();
  194.     if (theErr == noErr && theMirror == 0)
  195.         theErr = memFullErr;
  196.     nrequire (theErr, FailedToAllocateMirror);
  197.  
  198.     theErr = MBSaveString (ref->stringBuffer, (char *) &(mirrorName[1]), mirrorName[0],
  199.                             &(theMirror->oName));
  200.     nrequire (theErr, FailedToSaveName);
  201.  
  202.     theMirror->mirrorFlags = flags;
  203.  
  204.     /**
  205.      ** Add this mirror to the mirror list!
  206.      **/
  207.  
  208.     theMirror->next = ref->firstMirror;
  209.     ref->firstMirror = theMirror;
  210.     ref->listStatus |= kMLModified;
  211.     return noErr;
  212.  
  213.     /**
  214.      ** Errors
  215.      **/
  216.  
  217. FailedToSaveName:                        // Name would not be saved!
  218.  
  219.     DisposPtr ((Ptr) theMirror);
  220.  
  221. FailedToAllocateMirror:                    // Couldn't allocate space for mirror struct! :(
  222.  
  223. BadParams:
  224.  
  225.     return theErr;
  226. }
  227.  
  228. /**
  229.  ** _URLHelperNumberOfMirrors
  230.  **
  231.  **   For a given mirror list, this will return the number of usable mirrors
  232.  **      in the list.
  233.  **/
  234. pascal ComponentResult _URLHelperNumberOfMirrors (Handle storage,
  235.                                         URLHMirrorListPtr    ref,
  236.                                         short                *num)
  237. {
  238.     OSErr                theErr;
  239.     URLGlobalsHandle    globals = (URLGlobalsHandle) storage;
  240.     URLHMirrorPtr        p;
  241.     short                total;
  242.  
  243.     /**
  244.      ** Check the params
  245.      **/
  246.     
  247.     theErr = paramErr;
  248.     require (ref != 0 && num != 0, BadParams);
  249.  
  250.     /**
  251.      ** Ok.  Loop over the list, counting.
  252.      **/
  253.     total = 0;
  254.     p = ref->firstMirror;
  255.     while (p) {
  256.  
  257.         if (!(p -> mirrorFlags & kURLHMNoUse))
  258.             total++;
  259.  
  260.         p = p -> next;
  261.     }
  262.  
  263.     *num = total;
  264.  
  265.     return noErr;
  266.     
  267.     /**
  268.      ** Errors
  269.      **/
  270.  
  271. BadParams:
  272.  
  273.     *num = 0;
  274.     return theErr;
  275. }
  276.  
  277. /**
  278.  * _URLHelperGetUsableMirror
  279.  *
  280.  *  Loop through the mirrors around, and return one that can be used for subst.
  281.  *
  282.  **/
  283. pascal ComponentResult _URLHelperGetUsableMirror (Handle storage,
  284.                                         URLHMirrorListPtr    mirrorListRef,
  285.                                         URLHMirrorPtr        *mirrorRef,
  286.                                         short                index)
  287. {
  288.     OSErr                theErr;
  289.     URLHMirrorPtr        aMirror;
  290.  
  291.     /**
  292.      ** Check incomming params...
  293.      **/
  294.     
  295.     theErr = paramErr;
  296.     require (mirrorListRef != 0 && mirrorRef != 0 && index > 0, BadParams);
  297.     theErr = noErr;
  298.  
  299.     /**
  300.      ** Loop over the list.  Ignroe ones that have the nouse flag set...
  301.      **/
  302.     
  303.     aMirror = mirrorListRef -> firstMirror;
  304.     while (aMirror != 0) {
  305.         if (!(aMirror->mirrorFlags & kURLHMNoUse))
  306.             index--;
  307.         if (index == 0)
  308.             break;
  309.         aMirror = aMirror -> next;
  310.     }
  311.     
  312.     if (aMirror == 0)
  313.         theErr = errURLHNoUsableMirror;
  314.     nrequire (theErr, FailedToFindUsableMirror);
  315.  
  316.     *mirrorRef = aMirror;
  317.  
  318.     return noErr;
  319.     
  320.     /**
  321.      ** Handle errors
  322.      **/
  323.  
  324. FailedToFindUsableMirror:
  325.  
  326. BadParams:
  327.     *mirrorRef = 0;
  328.     
  329.     return theErr;
  330. }